//--------------------------------------------------------------------
//     Author:Howe
//     Date: 2021-04-13 21:46:32 
//     Description:对此文件的更改可能会导致不正确的行为，并且如果重新生成代码，这些更改将会丢失。
//--------------------------------------------------------------------
using System;
using System.Threading.Tasks;
using Kylin.Wiki.Core;
using Kylin.Wiki.Services;
using Kylin.Wiki.Model.Entity;
using Kylin.Wiki.IService;
using Kylin.Wiki.Core.Extensions.AutofacManager;
using Kylin.Wiki.Repository;
using Kylin.Wiki.Model.Api;
using System.Collections.Generic;
using Kylin.Wiki.Model.Dto;
using Kylin.Wiki.Core.Enum;
using System.Linq.Expressions;
using Mapster;

namespace Kylin.Wiki.Services
{
    /// <summary>
    /// RoleServices
    /// </summary>	
    public class RoleService : Service<Role>, IRoleService, IDependency
    {
        private readonly IUser_RoleService _user_RoleService;
        private readonly IRole_MenuService _role_MenuService; 


        public RoleService(IUnitOfWork unitOfWork, IRepository<Role> repository,
            IUser_RoleService user_RoleService, IRole_MenuService role_MenuService 
            ) : base(unitOfWork, repository)
        { 
            _user_RoleService = user_RoleService;
            _role_MenuService = role_MenuService;
        }

        public ApiResult<bool> Create(RoleUpdateDTO roleUpdateDTO)
        {
            var role = roleUpdateDTO.Adapt<Role>();
            base.Add(role, true);
            List<Role_Menu> role_Menus = new List<Role_Menu>();
            foreach (var item in roleUpdateDTO.MenuList)
            {
                Role_Menu role_Menu = new Role_Menu
                {
                    CreateBy = roleUpdateDTO.CreateBy,
                    CreateOn = DateTime.Now,
                    IsDelete = "0",
                    UpdateBy = roleUpdateDTO.UpdateBy,
                    UpdateOn = DateTime.Now,
                    MenuID = item,
                    RoleID = role.RoleID
                };
                role_Menus.Add(role_Menu);
            }
            var reuslt = _role_MenuService.AddRange(role_Menus, true);
            return new ApiResult<bool>(reuslt > 0, HttpStateCode.Success, "添加成功");
        }

        public ApiResult<bool> Delete(Role role)
        {
            if (_user_RoleService.Exists(x => x.RoleID == role.RoleID))
                return new ApiResult<bool>(false, HttpStateCode.Error, "该角色正在使用中");
            if (_role_MenuService.Exists(x => x.RoleID == role.RoleID))
                return new ApiResult<bool>(false, HttpStateCode.Error, "该角色正在使用中");
            return new ApiResult<bool>(base.Delete(role, true) > 0, HttpStateCode.Success, "删除成功");
        }

        public ApiPageResult<List<Role>> GetListByPage(RoleDTO roleDTO)
        {
            ApiPageResult<List<Role>> result = new ApiPageResult<List<Role>>();
            var orderBy = new Dictionary<string, QueryOrderBy>() { {
                    "OrderNum", QueryOrderBy.Asc
             } };
            var data = base.QueryablePage(roleDTO.PageIndex,
               roleDTO.PageSize,
               out int rowCount,
               x => x.Status == false,
               orderBy);
            result.Data = data;
            result.TotalCount = rowCount;
            result.PageIndex = roleDTO.PageIndex;
            result.PageSize = roleDTO.PageSize;
            result.TotalPages = (rowCount % roleDTO.PageSize) > 0 ? rowCount / roleDTO.PageSize + 1 : rowCount / roleDTO.PageSize;
            return result;
        }

        public ApiResult<bool> Update(RoleUpdateDTO roleUpdateDTO)
        {
            //判断是否存在该用户
            if (!base.Exists(x => x.RoleID == roleUpdateDTO.RoleID))
                return new ApiResult<bool>(false, HttpStateCode.Error, "该角色不存在");
            var role =  roleUpdateDTO.Adapt<Role>();
            var menus_role = _role_MenuService.Find(x => x.RoleID == roleUpdateDTO.RoleID);
            foreach (var item in menus_role)
            {
                _role_MenuService.Delete(item, false);
            }
            Expression<Func<Role, object>>[] updatedProperties =
            {
                   p=>p.RoleName,
                   p=>p.OrderNum,
                   p=>p.UpdateBy,
                   p=>p.UpdateOn
            };
            base.Update(role, updatedProperties, true);
            List<Role_Menu> role_Menus = new List<Role_Menu>();
            foreach (var item in roleUpdateDTO.MenuList)
            {
                Role_Menu role_Menu = new Role_Menu
                {
                    CreateBy = roleUpdateDTO.CreateBy,
                    CreateOn = DateTime.Now,
                    IsDelete = "0",
                    UpdateBy = roleUpdateDTO.UpdateBy,
                    UpdateOn = DateTime.Now,
                    MenuID = item,
                    RoleID = role.RoleID
                };
                role_Menus.Add(role_Menu);
            }
            var reuslt = _role_MenuService.AddRange(role_Menus, true);
            return new ApiResult<bool>(reuslt > 0, HttpStateCode.Success, "修改成功");
        }
    }

}
